package com.chtw.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chtw.entity.LoginAccount;
import com.chtw.entity.TeacherInfo;
import com.chtw.service.LoginAccountService;
import com.chtw.service.TeacherService;
import com.chtw.utils.POIUtils;
import com.chtw.utils.Result;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/teacher")
public class TeacherInfoContriller {

	@Autowired
	private TeacherService teacherService;
	
	
	@RequestMapping("/getpage")
	@ResponseBody
	public Result getpage(@RequestParam(defaultValue="1",required=false)String nowpage,TeacherInfo teacherInfo) {
		PageInfo<TeacherInfo> info = teacherService.page(Integer.parseInt(nowpage),teacherInfo);
		List<TeacherInfo> tList = info.getList();
		if(tList!=null) {
			return Result.seccess(tList,info.getPages(),(int)info.getTotal());
		}
		return Result.fail("获取失败！");
	}
	
	/**
	 * 获取入职时间
	 * @return
	 */
	@RequestMapping("/getintime")
	@ResponseBody
	public Result getintime() {
		List<TeacherInfo> tnoList = teacherService.getListByTime();
		if(tnoList.size()>0) {
			return Result.seccess(tnoList);
		}
		return Result.fail("获取失败！");
	}
	
	/**
	 * 删除
	 * @param teachId
	 * @param session
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(String teachId,HttpSession session) {
		int i = teacherService.delete(Integer.parseInt(teachId));
		PageInfo<TeacherInfo> info = teacherService.page(1);
		if(i>0) {
			session.setAttribute("tpages", info.getPages());
			return Result.seccess();
		}else {
			return Result.fail("删除失败！");
		}
	}
	
	/**
	 * 按照teachId查询
	 * @param teachId
	 * @return
	 */
	@RequestMapping("/getinfo")
	@ResponseBody
	public Result getinfo(String teachId,HttpSession session) {
		if(teachId.equals("00000")) {
			teachId = (String)session.getAttribute("tcurrentUser");
		}
		TeacherInfo teacher = teacherService.getinfo(Integer.parseInt(teachId));
		if(teacher!=null) {
			return Result.seccess(teacher);
		}else {
			return Result.fail("查询失败！");
		}
	}
	
	/**
	 * 修改数据
	 * @param teach
	 * @return
	 */
	@RequestMapping("/updateteach")
	@ResponseBody
	public Result updateteach(TeacherInfo teach) {
		int i = teacherService.updateteach(teach);
		if(i>0) {
			return Result.seccess();
		}else {
			return Result.fail("修改失败");
		}
	}
	
	/**
	 * 添加
	 * @param teach
	 * @param session
	 * @return
	 */
	@RequestMapping("/addteach")
	@ResponseBody
	public Result addteach(TeacherInfo teach,HttpSession session) {
		int i = teacherService.addteach(teach);
		PageInfo<TeacherInfo> info = teacherService.page(1);
		if(i>0) {
			session.setAttribute("tpages", info.getPages());
			return Result.seccess();
		}else {
			return Result.fail("添加失败！");
		}
	}
	/**
	 * 导出文件
	 * @param teach
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/outFile")
	public void outfile(TeacherInfo teach,HttpServletResponse response) throws IOException {
		List<TeacherInfo> tList = teacherService.search(teach);
		String data = JSON.toJSONString(tList);
		//System.out.println(data);
		JSONArray body = JSON.parseArray(data);
		List<Object> json = new ArrayList<Object>();
		json.add("教师工号");
		json.add("姓名");
		json.add("入职时间");
		json.add("性别");
		json.add("职务编号");
		json.add("薪资");
		
		JSONArray head = new JSONArray(json);
		HSSFWorkbook wbook = POIUtils.expExcel(head,body,"教职工信息");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setLocale(new java.util.Locale("zh","CN"));
		response.setContentType("application/octet-stream");
		String fileName = URLEncoder.encode("教职工信息.xls","UTF-8");
		response.addHeader("Content-Disposition","attachment;fileName="+fileName);
		OutputStream os = response.getOutputStream();
		wbook.write(os);
		os.close();
	}
}
